/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.admin.model;

import com.common.json.JsonModel;
import com.entities.EventRegistration;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.StoredProcedureQuery;
import org.json.simple.JSONObject;

/**
 *
 * @author Tuan Long
 */
@Stateless
public class EventRegistrationFacade extends AbstractFacade<EventRegistration> implements EventRegistrationFacadeLocal {
    @PersistenceContext(unitName = "BluePumpkinEJBPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public EventRegistrationFacade() {
        super(EventRegistration.class);
    }

    /**
     * @EventRegistrationFacade.fillTotalRegistered: return to List< -JSONObject
     * String->, with a JSONObject is the row-result of the Select statement
     * from database
     * @return List<>
     */
    @Override
    public List<String> fillTotalRegistered() {        
        List<String> jsonlist = new ArrayList<String>();
        StoredProcedureQuery query = em.createNamedStoredProcedureQuery("EventRegistration.totalRegistered");        
        List<Object[]> results = query.getResultList();
        for (Object[] row : results) {            
            String username = (String) row[0];                        
            String total =(String) row[1];
            JSONObject jsonObj = JsonModel.eventTotalRegistrationSender(username,total);
            String json = jsonObj.toString(); 
            jsonlist.add(json); 
            System.out.println("------------JSON : "+json);
        }
        return jsonlist;
    }
    
}
